package level.editor.tool.tile;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

import level.editor.command.Command;
import level.editor.main.LevelHelper;
import level.editor.tool.SetToolCommand;
import level.editor.tool.Tool;
import level.editor.tool.ToolView;
import level.state.Tile;

public class ToolTile extends JPanel implements Tool, ActionListener,
		ItemListener
{
	private ToolTileSelect select;
	private ToolTileButton selectedButton;
	private ToolView view;
	private String layerName;

	public ToolTile(ToolView view)
	{
		super(new BorderLayout());
		setPreferredSize(new Dimension(400, 400));
		select = null;
		selectedButton = null;
		layerName = null;
		this.view = view;
		init();
	}

	public ToolTileButton getSelectedButton()
	{
		return selectedButton;
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (e.getActionCommand().equals("Directory"))
		{
			directoryAction((ToolTileDirectory) e.getSource());
		}

		if (e.getActionCommand().equals("Tile"))
		{
			selectAction((ToolTileButton) e.getSource());
		}
	}

	@Override
	public void itemStateChanged(ItemEvent e)
	{
		if (e.getStateChange() == ItemEvent.SELECTED)
			layerName = (String) e.getItem();
	}

	@Override
	public void pushUpCommand(Command command)
	{
		view.pushUpCommand(command);
	}

	@Override
	public void pushDownCommand(Command c)
	{
		System.out.println("ERROR: pushDownCommand in ToolTile");
	}

	@Override
	public void handleClick(MouseEvent e)
	{

	}

	@Override
	public void handlePress(MouseEvent e)
	{
		if (selectedButton != null)
		{
			Tile tile = selectedButton.getTile().cloneTile();
			tile.setX(LevelHelper.snapToGrid(e.getPoint()).x);
			tile.setY(LevelHelper.snapToGrid(e.getPoint()).y);
			pushUpCommand(new TileAddCommand(tile, layerName));
		}
	}

	private void selectAction(ToolTileButton button)
	{
		if (button != null)
		{
			selectedButton = button;
			pushUpCommand(new SetToolCommand(this));
		}
	}

	private void directoryAction(ToolTileDirectory directory)
	{
		if (directory != null)
		{
			select.updateTiles(directory.getDir() + "\\"
					+ (String) directory.getSelectedItem());
		}
	}

	private void init()
	{
		initDirectory("C:/Users/Huu/workspace/DropBox/Resources/Images/Tiles");
		initSelect();
	}

	private void initDirectory(String dir)
	{
		ToolTileDirectory directory = new ToolTileDirectory(dir, this);
		ToolTileLayer layer = new ToolTileLayer(this);
		JPanel directoryPanel = new JPanel();
		directoryPanel.add(new JLabel("Tileset Loader"));
		directoryPanel.add(directory);
		directoryPanel.add(new JLabel("            Layer Selecter"));
		directoryPanel.add(layer);
		add(directoryPanel, BorderLayout.NORTH);
	}

	private void initSelect()
	{
		ToolTileSelect select = new ToolTileSelect(this);
		this.select = select;
		JPanel selectPanel = new JPanel(new BorderLayout());
		selectPanel.setBorder(BorderFactory.createTitledBorder("Tileset"));
		selectPanel.add(select, BorderLayout.CENTER);
		add(selectPanel, BorderLayout.CENTER);
	}
}
